<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="addOne">添加权限</el-button>
      <!--
        table 支持树形结构
         1. row-key=“id” 树形节点添加唯一标识
         2.默认展开 default-expand-all 展开所有
        -->
      <el-table :data="list" default-expand-all row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template #default="{ row }">
            <!-- 一级权限 type === 1 才能添加子权限 -->
            <!-- 二级权限 type === 2 控制页面中的按钮是否显示 / 禁用 -->
            <el-button v-if="row.type === 1" size="mini" type="text" @click="addTwo(row.id)">添加</el-button>
            <el-button size="mini" type="text" @click="btnEditRow(row)">编辑</el-button>
            <el-popconfirm title="确定删除该权限吗？" @onConfirm="delPermission(row.id)">
              <el-button slot="reference" size="mini" type="text" style="margin-left: 10px">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="添加权限" :visible="showDialog" width="40%" @close="showDialog = false">
      <el-form ref="form" label-width="80px" :model="form" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="form.description" type="textarea" />
        </el-form-item>
        <el-form-item label="开启" prop="enVisible">
          <el-switch v-model="form.enVisible" active-value="1" inactive-value="0" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" @click="confirm">确定</el-button>
              <el-button @click="close">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getPermissionList, addPermission, delPermission, updatePermission } from '@/api/permission'
import { listToTree } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      form: {
        name: '',
        code: '',
        description: '',
        enVisible: '',
        type: null,
        pid: null
      },
      isEdit: false,
      rules: {
        name: [
          { required: true, message: '请输入权限名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入权限标识', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取权限列表
    async getPermissionList() {
      const res = await getPermissionList()
      // console.log(res)
      // 转换为树形结构
      this.list = listToTree(res, 0)
    },
    // 确认按钮
    async confirm() {
      // 表单校验
      await this.$refs.form.validate()
      if (!this.isEdit) {
        // 调用新增接口
        await addPermission(this.form)
        // 提示用户
        this.$message.success('添加权限成功')
      } else {
        // 调用编辑接口
        await updatePermission(this.form)
        // 提示用户
        this.$message.success('编辑权限成功')
      }
      // 重置表单+关闭弹层
      this.close()
      // 刷新列表
      this.getPermissionList()
    },
    // 添加一级权限
    addOne() {
      this.showDialog = true
      this.form.type = 1
      this.form.pid = 0
    },
    // 添加二级权限
    addTwo(id) {
      this.showDialog = true
      this.form.type = 2
      this.form.pid = id
    },
    // 取消按钮
    close() {
      // 关闭弹层
      this.showDialog = false
      // 清除表单数据
      this.$refs.form.resetFields()
    },
    // 删除权限
    delPermission(id) {
      delPermission(id)
      this.$message.success('删除成功')
      this.getPermissionList()
    },
    // 编辑按钮
    btnEditRow(row) {
      this.showDialog = true
      this.form = {
        ...row
      }
      this.isEdit = true
    }
  }
}
</script>
<style>
.btn-add {
  margin: 10px;
}
</style>
